<!--全部图书信息-->
<html>
<head>
    <meta charset="utf-8">
    <title>全部图书信息</title>
    <!--<link rel="stylesheet" href="css/bootstrap.min.css">-->
    <!--<script src="js/jquery-3.3.1.js"></script>-->
    <!--<script src="js/bootstrap.min.js" ></script>-->
    <!--<script src="js/getParameter.js"></script>-->
    <style>
        body{
            background-color: rgb(240,242,245);
        }
        .ssk{
            margin: 10px auto;
            text-align: center;
        }
    </style>
</head>
<body>

<div class="ssk">
    <form class="form-inline"  id="searchform">
        <div class="input-group">
           <input type="text" placeholder="输入图书名" class="form-control" id="searchBook" name="searchWord" class="form-control">
            <span class="input-group-btn">
                <input type="submit" value="搜索" class="btn btn-default">
            </span>
        </div>
    </form>
</div>
<div style="position: relative;top: 10%">
<!--<c:if test="${!empty succ}">-->
    <!--<div class="alert alert-success alert-dismissable">-->
        <!--<button type="button" class="close" data-dismiss="alert"-->
                <!--aria-hidden="true">-->
            <!--&times;-->
        <!--</button>-->
        <!--${succ}-->
    <!--</div>-->
<!--</c:if>-->
<!--<c:if test="${!empty error}">-->
    <!--<div class="alert alert-danger alert-dismissable">-->
        <!--<button type="button" class="close" data-dismiss="alert"-->
                <!--aria-hidden="true">-->
            <!--&times;-->
        <!--</button>-->
        <!--${error}-->
    <!--</div>-->
<!--</c:if>-->
</div>
<div class="panel panel-default" style="width: 90%;margin-left: 5%">
    <div class="panel-heading" style="background-color: #fff">
        <h3 class="panel-title">
            全部图书
        </h3>
    </div>
    <div class="panel-body">
        <table class="table table-hover">
            <thead>
            <tr>
                <th>书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>ISBN</th>
                <th>价格</th>
                <th>借还</th>
                <th>详情</th>
                <th>编辑</th>
                <th>删除</th>
            </tr>
            </thead>
            <tbody id="table_book"></tbody>
        </table>
        <div class="page_num_inf">
            <i></i> 共
            <span id="totalPage_span">12</span>页<span id="totalCount_span">132</span>本
        </div>
        <!--分页按钮-->
        <nav aria-label="Page navigation">
            <ul id="page_ul" class="pagination">
                <li>
                    <a href="#" aria-label="Previous">
                        <span aria-hidden="true">上一页</span>
                    </a>
                </li>
                <li id=""></li>
                <li>
                    <a href="#" aria-label="Next">
                        <span aria-hidden="true">下一页</span>
                    </a>
                </li>
            </ul>
        </nav>
    </div>
</div>

</body>
<script>
    var bookId;
    //姚奇提供的接口:从你分页查询到的list集合中的bookInfo对象
    //1.拼接按钮时访问user/judge帮你判断是拼归还还是借阅按钮，1你需要给我传递书名,2你需要接收一个Integer类型的数据state，state=1拼借阅，state=0拼归还。
    //你需要提供给我的接口：
    // 1.点击*借阅按钮*访问并把bookid带给我,admin_book_lend.html？bookId=...你需要接收我返回给你的get请求里面有"图书借阅成功"这个信息你提示在页面上，你用js的getParameter("msg")接收
    //2.点击*还书按钮*通过ajax访问并把bookid带给我,user/returnBook,你需要接收我返回给你的get请求里面有"图书归还成功"这个信息你提示在页面上，你用js的getParameter("msg")接收
    function mySubmit(flag){
        return flag;
    }
    $("#searchform").submit(function () {
        searchBookFuc();
        return false;
    });
    function searchBookFuc() {
        bookId = $.cookie("bookId");
        var searchBook = $("#searchBook").val();
        if(searchBook){
            searchBook = encodeURI(searchBook);
        }
        loads(bookId,1,10,searchBook);
    }
    $(function () {
        loads(null,1,10,null);
    });
    function loads(bookId, currentPage, rows,searchBook) {
        $.ajax({
            type:"post",
            dataType:"json",
            url:"book/findAllBook",
            data:{"bookId":bookId,"currentPage":currentPage,"rows":rows,"searchBook":searchBook},
            success:function (data) {
                window.scrollTo(0,0);
                $("#totalPage_span").html(data.data.totalPage);
                $("#totalCount_span").html(data.data.totalCount);
                if(data.data.list.length == 0){
                    alert("未查到书籍");
                    $("#searchBook").val("");
                    load(null,1,10,null);
                }
                var trs = '';
                $.each(data.data.list,function (index,ele) {
                    trs += '<tr>\n' +
                        '                <td>'+ele.name+'</td>\n' +
                        '                <td>'+ele.author+'</td>\n' +
                        '                <td>'+ele.publish+'</td>\n' +
                        '                <td>'+ele.isbn+'</td>\n' +
                        '                <td>'+ele.price+'</td>\n';
                    if(ele.state == 1){
                        trs += '<td><a class="jump2" name="admin_book_lend.html?bookName='+ele.name+'"><button type="button" class="btn btn-primary btn-xs">借阅</button></a></td>\n';
                    }
                    if(ele.state == 0) {
                        trs += '<td><a class="jump2" name="user/returnBook?book_id='+ele.book_id+'"><button type="button" class="btn btn-primary btn-xs">归还</button></a></td>\n';
                    }
                    trs += '<td><a class="jump2" name="admin_book_detail.html?bookId='+ele.book_id+'"><button type="button" class="btn btn-success btn-xs">详情</button></a></td>\n' +
                        '<td><a class="jump2" name="admin_book_edit.html?bookId='+ele.book_id+'"><button type="button" class="btn btn-info btn-xs">编辑</button></a></td>\n' +
                        '<td><a class="jump2" name="book/deleteBook?bookId='+ele.book_id+'"><button type="button" class="btn btn-danger btn-xs">删除</button></a></td>\n' +
                        '</tr>';

                });
                $("#table_book").html(trs);
                $(".jump2").click(function () {
                    setCookies($(this).prop("name"));
                    $("#middle").load($(this).prop("name"));
                });
                //分页按钮
                var pagelis = '';
                pagelis +=' <li>\n' +
                    '                    <a href="javascript:loads('+bookId+','+((data.data.currentPage-1)<=1?1:(data.data.currentPage-1))+','+data.data.rows+',\''+searchBook+'\')" aria-label="Previous">\n' +
                    '                        <span aria-hidden="true">上一页</span>\n' +
                    '                    </a>\n' +
                    '                </li>';
                var begin = 1;
                var end = 10;
                if (data.data.totalPage < 10){
                    begin = 1;
                    end = data.data.totalPage;
                } else{
                    begin = data.data.currentPage -5;
                    end = data.data.currentPage + 4 ;
                    if (begin < 1){
                        begin = 1;
                        end = begin + 9;
                    }
                    if (end > data.data.totalPage) {
                        end = data.data.totalPage;
                        begin = end -9;
                    }
                }
                for (var i = begin; i <= end; i++) {

                    // pagelis += '<li><a href="#">'+i+'</a></li>';

                    if(i == data.data.currentPage){
                        pagelis += '<li class="active"><a href="javascript:loads('+bookId+','+i+','+data.data.rows+',\''+searchBook+'\')">'+i+'</a></li>';
                    }else{
                        pagelis += '<li><a href="javascript:loads('+bookId+','+i+','+data.data.rows+',\''+searchBook+'\')">'+i+'</a></li>';
                    }
                }
                pagelis += '<li>\n' +
                    '                    <a href="javascript:loads('+bookId+','+((data.data.currentPage+1)>=data.data.totalPage?data.data.totalPage:(data.data.currentPage+1))+','+data.data.rows+',\''+searchBook+'\')" aria-label="Next">\n' +
                    '                        <span aria-hidden="true">下一页</span>\n' +
                    '                    </a>\n' +
                    '                </li>';
                $("#page_ul").html(pagelis)
            }
        })
    }
</script>
</html>;
